package com.huayue.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.WebInfo;
import com.huayue.service.WebInfoService;
import com.huayue.util.FileUploadHelper;

/**
 * 网站基本信息设置
 */
@Controller
@RequestMapping("/webInfo")
public class WebInfoController {

    @Autowired
    WebInfoService svc;

    BaseController bc = new BaseController();

    @LogAnno(operateType = "修改-网站设置")
    @RequestMapping("/update")
    public ModelAndView update(WebInfo webInfo) {
        return bc.update(svc, webInfo);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(WebInfo webInfo) {
        return bc.findOne(svc, webInfo);
    }

    @LogAnno(operateType = "修改-网站LOGO")
    @RequestMapping("/uploadIcon")
    public ModelAndView uploadIcon(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        String fileName = FileUploadHelper.fileUpload(file, req);
        if (fileName != null) {
            WebInfo info = new WebInfo();
            info.setIcon(fileName);
            if (svc.updateIcon(info) > 0) {
                view.addStaticAttribute("icon", "1");
                view.addStaticAttribute("msg", "文件上传成功！");
                return new ModelAndView(view);
            }
        }
        view.addStaticAttribute("icon", "2");
        view.addStaticAttribute("msg", "文件上传失败！");
        return new ModelAndView(view);
    }
}
